package com.letv.core.sysletvplayer.base.impl;

import android.app.Activity;
import android.content.res.Configuration;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnInfoListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnSeekCompleteListener;
import android.media.MediaPlayer.OnVideoSizeChangedListener;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.MediaController;

import com.letv.component.player.LetvMediaPlayerControl;
import com.letv.component.player.LetvVideoViewBuilder;
import com.letv.component.player.LetvVideoViewBuilder.Type;
import com.letv.component.player.Interface.OnVideoViewStateChangeListener;
import com.letv.android.sdk.R;
import com.letv.core.sysletvplayer.base.BaseFragment;

public class LocalPlayerFragment extends BaseFragment {
	
	private Activity activity;
	private ViewGroup mVideoViewContain;
	private LetvMediaPlayerControl control = null;
	private boolean isPlayingOrStart = false; // ture:播放状态  false：暂停状态
	private int curTime;

	public void setPlayingOrStart(boolean isPlayingOrStart) {
		this.isPlayingOrStart = isPlayingOrStart;
	}

	@Override
	public void onAttach(Activity activity) {
		Log.i("chenyg", "LocalPlayerFragment--->onAttach()");
		this.activity = activity;
		super.onAttach(activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.i("chenyg", "LocalPlayerFragment--->onCreate()");
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.i("chenyg", "LocalPlayerFragment--->onCreateView()");
		mVideoViewContain = (ViewGroup) inflater.inflate(R.layout.letv_local_player_fragment, null);
		control = LetvVideoViewBuilder.getInstants()
									  .build(activity.getApplicationContext(), Type.MOBILE_H264_MP4);
		LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT);
		mVideoViewContain.addView(control.getView(), params);
		Log.i("chenyg", "LocalPlayerFragment--->onCreateView()");
		return mVideoViewContain;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		Log.i("chenyg", "LocalPlayerFragment--->onActivityCreated()");
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public void onResume() {
		Log.i("chenyg", "LocalPlayerFragment--->onResume():curTime=" + curTime);
		Log.i("chenyg", "isPlayingOrStart===============" + isPlayingOrStart);
		if (control != null) {
			if (curTime > 0) {
				control.seekTo(curTime);
			}
			if (isPlayingOrStart) {
				control.start();
			}
		}
		super.onResume();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		Log.i("chenyg", "LocalPlayerFragment--->onConfigurationChanged()");
	}

	@Override
	public void onPause() {
		Log.i("chenyg", "LocalPlayerFragment--->onPause():curTime=" + curTime);
		if(control != null){
        	control.pause();
        }
		curTime = control.getCurrentPosition();
		super.onPause();
	}

	@Override
	public void onDestroyView() {
		Log.i("chenyg", "LocalPlayerFragment--->onDestroyView()");
		super.onDestroyView();
	}

	@Override
	public void onDestroy() {
		Log.i("chenyg", "LocalPlayerFragment--->onDestroy()");
		super.onDestroy();
		stopPlayback();
	}

	@Override
	public void onDetach() {
		Log.i("chenyg", "LocalPlayerFragment--->onDetach()");
		super.onDetach();
	}
	
	
	/**
	 * 播放,兼容在线与本地播放器
	 * */
	public void playLocal(String uriString, int msec) {
		control.setVideoPath(uriString);
		if (msec > 0) {
			control.seekTo(msec);
		}
		control.start();
		isPlayingOrStart = true;
	}

	/**
	 * 播放
	 * */
	public void start() {
        if(control != null){
        	control.start();
        	isPlayingOrStart = true;
        }
	}
	
	/**
	 * 暂停
	 * */
	public void pause() {
        if(control != null){
        	control.pause();
        	isPlayingOrStart = false;
        	curTime = control.getCurrentPosition();
        }
	}
	
	/**
	 * 停止
	 * */
	public void stopPlayback() {
        if(control != null){
        	control.stopPlayback();
        }
	}

	/**
	 * 跳到
	 * */
	public void seekTo(int msec) {
        if(control != null){
        	control.seekTo(msec);
        }
	}

	/**
	 * 快进
	 * */
	public void forward() {
        if(control != null){
        	control.forward();
        }
	}
	
	/**
	 * 快退
	 * */
	public void rewind() {
        if(control != null){
        	control.rewind();
        }
	}

	/**
	 * 是否在播放中
	 * */
	public boolean isPlaying() {
		return control.isPlaying();
	}
	
	/**
	 * 是否进入播放状态（包括准备完成，播放，暂停，完成等）
	 * */
	public boolean isInPlaybackState() {
		return control.isInPlaybackState();
	}

	/**
	 * 得到当前时间点
	 * */
	public int getCurrentPosition() {
		return control.getCurrentPosition();
	}

	/**
	 * 得到总时长
	 * */
	public int getDuration() {
		return control.getDuration();
	}
	
	public void setMediaController(MediaController controller) {
		if(control != null){
        	control.setMediaController(controller);
        }
	}
	
	public void setOnPreparedListener(OnPreparedListener l) {
		if(control != null){
        	control.setOnPreparedListener(l);
        }
	}
	
	public void setOnCompletionListener(OnCompletionListener l) {
		if(control != null){
        	control.setOnCompletionListener(l);
        }
	}
	
	public void setOnInfoListener(OnInfoListener l) {
		if(control != null){
        	control.setOnInfoListener(l);
        }
	}
	
	public void setOnErrorListener(OnErrorListener l) {
		if(control != null){
        	control.setOnErrorListener(l);
        }
	}
	
	public void setOnSeekCompleteListener(OnSeekCompleteListener l) {
		if(control != null){
        	control.setOnSeekCompleteListener(l);
        }
	}
	
	public void setOnVideoSizeChangedListener(OnVideoSizeChangedListener l) {
		if(control != null){
        	control.setOnVideoSizeChangedListener(l);
        }
	}
	
	public void setOnBufferingUpdateListener(OnBufferingUpdateListener l) {
		if(control != null){
        	control.setOnBufferingUpdateListener(l);
        }
	}
	
	public void setVideoViewStateChangeListener(OnVideoViewStateChangeListener l) {
		if(control != null){
        	control.setVideoViewStateChangeListener(l);
        }
	}

}
